import { ui } from "../../../ui/layaMaxUI";
import MonsterData from "../../../core_wq/game/data/MonsterData";
import GameEvent from "../../../core_wq/game/GameEvent";



export default class HpBarView extends ui.moduleView.common.HpBarViewUI {


    private _rect: Laya.Rectangle = new Laya.Rectangle();

    private _curValue: number;

    private _maxValue: number;

    private shape: Laya.Sprite;

    private _skin: string;

    private _scale: any;


    public init(value: number, skin: number, scale: any): void {
        this._curValue = value;
        this._maxValue = value;
        this._skin = "images/common/m_hp" + skin + ".png";
        this._scale = scale;

    }

    public reset() {
        this._rect.width = this.width;
        this._curValue = this._maxValue;
        // console.log("reset",this._skin)
        this.scale(this._scale.x, this._scale.y);
        if(this.bar){
            this.bar.skin = this._skin;
        }
    }
 
  
    public onAwake(){
        if (this.bar &&this._curValue) {
            this.scale(this._scale.x, this._scale.y);
            this.bar.skin = this._skin;
        }
    }

    public update(hurt: number): void {
        //   console.log("hit hpbarview..",hurt,this._curValue)
        if (!this.bar && !this._curValue) {
            return;
        }
        // console.log("update",hurt)
        if (!this.shape) {
            this.shape = new Laya.Sprite();
            this.shape.y = this.bar.y + 2;
        }
        this._rect.x = 0;
        this._rect.y = 0;
        this._rect.height = 17;

        this._curValue -= hurt;
        this._curValue = Math.max(this._curValue, 0);
        this._rect.width = this._curValue / this._maxValue * this.width;
        this.bar.scrollRect = this._rect;
        // console.log("_monsterData ",this._monsterData)
        Laya.Tween.clearTween(this.shape);
        this.shape.graphics.clear();
        this.shape.graphics.drawRect(0, 0, hurt / this._maxValue * this.width, 13, "#ffffff");
        this.addChild(this.shape);
        this.shape.x = this._rect.width;
        Laya.Tween.to(this.shape, { width: 0 }, 300, null, new Laya.Handler(this, this.onCom));

        if (this._curValue == 0) {
            this.removeSelf();
        }
    }


    private onCom(): void {
        this.shape.removeSelf();
    }

}